import {
  createParamDecorator,
  ExecutionContext,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

export interface AuthUser {
  id: string;
  username: string;
  email: string | null;
  wxId: string | null;
  createdAt: Date;
  updatedAt: Date;
}
interface Request {
  user: AuthUser;
}
type AuthUserKeys = keyof AuthUser;

export const RequireLogin = () => UseGuards(AuthGuard('jwt'));

export const UserInfo = createParamDecorator(
  (data: AuthUserKeys, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest<Request>();

    if (!request.user) {
      return null;
    }
    return data ? request.user[data] : request.user;
  },
);
